blob: fa227e0c12cacdf4024061f14fdafcabdde18b86 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
---
import For from '../components/For.astro';
import Show from '../components/Show.astro';
import Story from '../components/Story.astro';
import Layout from '../layouts/Layout.astro';
import fetchAPI from '../lib/api';
import type { IStory } from '../types.js';
const mapStories = {
top: 'news',
new: 'newest',
show: 'show',
ask: 'ask',
job: 'jobs',
};
function safeParseInt(value: any, fallback: number) {
try {
return parseInt(value) || fallback;
} catch {
return fallback;
}
}
const page = safeParseInt(Astro.url.searchParams.get('page'), 1);
const type =
Astro.params.stories && Astro.params.stories in mapStories
? (Astro.params.stories.toString() as keyof typeof mapStories)
: 'top';
const stories = (await fetchAPI(`${mapStories[type]}?page=${page}`)) as IStory[];
---
<Layout>
<section>
<nav aria-labelledby="current-page">
<Show when={page > 1}>
<a href={`/${type}?page=${page - 1}`} aria-label="Previous Page"> < prev</a>
<span slot="fallback" aria-disabled="true"> < prev</span>
</Show>
<span id="current-page">page {page}</span>
<Show when={stories?.length >= 29}>
<a href={`/${type}?page=${page + 1}`} aria-label="Next Page">more ></a>
<span slot="fallback" aria-disabled="true"> more ></span>
</Show>
</nav>
<main>
<Show when={stories}>
<ul>
<For each={stories}>{(story: IStory) => <Story story={story} />}</For>
</ul>
</Show>
</main>
</section>
</Layout>
<style>
section {
padding-top: 45px;
}
nav,
main {
background-color: rgb(248 250 252);
border-radius: 2px;
}
nav {
padding: 15px 30px;
position: fixed;
text-align: center;
top: 55px;
left: 0;
right: 0;
z-index: 998;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}
nav a {
margin: 0 1em;
}
[aria-disabled='true'] {
color: rgb(71 85 105);
margin: 0 1em;
}
main {
position: absolute;
margin: 30px 0;
width: 100%;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
@media (max-width: 600px) {
main {
margin: 10px 0;
}
}
</style>
|